/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Text;

using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Shell;
using DslEditorPowerToy.Languages.EditorConfiguration;

namespace DslEditorPowerToy.Languages.EditorConfiguration
{
    #region Menu Handling
    partial class EditorConfigurationCommandSet
    {
        #region Private Properties
        /// <summary>
        /// Gets the model behind the current diagram
        /// </summary>
        public DslDomain CurrentModel
        {
            get
            {
                return ((DslDomain)this.CurrentDocView.CurrentDiagram.ModelElement);
            }
        }
        #endregion

        #region Protected Methods
        /// <summary>
        /// Adds menus to the IDE and context menus of the designer
        /// </summary>
        /// <returns></returns>
        protected override IList<MenuCommand> GetMenuCommands()
        {
            IList<MenuCommand> commands = base.GetMenuCommands();

            MenuCommand menuCommand = null;

            //Add Custom Menu Commands
            menuCommand = new DynamicStatusMenuCommand(
                new EventHandler(OnMenuStatus),
                new EventHandler(OnMenuAction),
                new CommandID(new Guid(Constants.EditorConfigurationCommandSetId),
                CustomCommands.MenuId.NewView));
            commands.Add(menuCommand);

            return commands;
        }
        #endregion

        #region Event Handlers
        
        /// <summary>
        /// Executes the menu command action
        /// </summary>
        /// <param name="sender">The source of the event</param>
        /// <param name="e">The event parameters</param>
        private void OnMenuAction(object sender, EventArgs e)
        {
            MenuCommand menuCommand = sender as MenuCommand;

            //Ensure correct Command Set
            if (false == menuCommand.CommandID.Guid.Equals(new Guid(Constants.EditorConfigurationCommandSetId)))
                return;

            //Get model object behind selected element
            //ModelElement selectedElement = ModelHelper.GetRepresentedElement(this.SingleSelection);

            //Check CommandId
            switch (menuCommand.CommandID.ID)
            {
                case CustomCommands.MenuId.NewView:
                    CustomCommands.CreateNewView(this.CurrentModel);
                    break;

                default:
                    //Do Nothing
                    break;
            }
        }

        /// <summary>
        /// Initialises the menu command
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnMenuStatus(object sender, global::System.EventArgs e)
        {
            MenuCommand menuCommand = sender as MenuCommand;

            //Ensure correct Command Set
            if (false == menuCommand.CommandID.Guid.Equals(new Guid(Constants.EditorConfigurationCommandSetId)))
                return;

            //Get current selection model elements
            //ArrayList selectedElements = new ArrayList((ICollection)ModelHelper.GetRepresentedElements(this.CurrentSelection));

            //Check CommandId
            switch (menuCommand.CommandID.ID)
            {
                case CustomCommands.MenuId.NewView:
                    menuCommand.Enabled = true;
                    menuCommand.Visible = true;
                    break;

                default:
                    //Unknown command
                    menuCommand.Enabled = false;
                    menuCommand.Visible = true;
                    break;
            }
        }
        #endregion
    }
    internal class CustomCommands
    {
        #region Menu Identifiers
        public class MenuId
        {
            public const int NewView = 0x510;
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Creates a new view
        /// </summary>
        public static void CreateNewView(DslDomain domain)
        {
            //Add a new view
            using (Transaction transaction = domain.Store.TransactionManager.BeginTransaction(Resources.Transaction_AddNewDslEditor))
            {
                domain.DslEditors.Add(new DslEditor(domain.Store, null));
                transaction.Commit();
            }
        }
        #endregion
    }

    #endregion
}
